﻿using CommandProcessor.Command;
using Core.Common;
using Data.Infrastructure;
using Data.Repositories;
using Domain.Commands;
using Domain.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data.Models;

namespace Domain.Handlers
{
    //Dung de check add
    public class CanAddToolType : IValidationHandler<CreateOrUpdateToolTypeCommand>
    {
        private readonly IToolTypeRepository ToolTypeRepository;
        public CanAddToolType(IToolTypeRepository ToolTypeRepository, IUnitOfWork unitOfWork)
        {
            this.ToolTypeRepository = ToolTypeRepository;
        }
        public IEnumerable<ValidationResult> Validate(CreateOrUpdateToolTypeCommand command)
        {
            //TODO
            //Neu Atributes (System.ComponentModel.DataAnnotations) khong thoa man cac dieu kien validation thi ban co
            //the them cac validation o day. Vd:
            if (command.Name == null)
                yield return new ValidationResult("Name", string.Format(Resources.Name));
        }
        public IEnumerable<ValidationResult> ValidateRange(IEnumerable<CreateOrUpdateToolTypeCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de insert
    public class CreateOrUpdateToolTypeHandler : ICommandHandler<CreateOrUpdateToolTypeCommand>
    {
        private readonly IToolTypeRepository ToolTypeRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateToolTypeHandler(IToolTypeRepository ToolTypeRepository, IUnitOfWork unitOfWork)
        {
            this.ToolTypeRepository = ToolTypeRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateToolTypeCommand command)
        {
            var ToolType = new ToolType
            {
                ID = command.ID,
                Description = command.Description,
                Name = command.Name,
                Code = command.Code,
                UOMID=command.UOMID,
                Function=command.Function
            };
            if (ToolType.ID == 0)
                ToolTypeRepository.Add(ToolType);
            else
                ToolTypeRepository.Update(ToolType);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateToolTypeCommand> commands)
        {
            throw new NotImplementedException();
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateToolTypeCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de delete
    public class DeleteToolTypeHandler : ICommandHandler<DeleteToolTypeCommand>
    {
        private readonly IToolTypeRepository ToolTypeRepository;
        private readonly IUnitOfWork unitOfWork;
        public DeleteToolTypeHandler(IToolTypeRepository ToolTypeRepository, IUnitOfWork unitOfWork)
        {
            this.ToolTypeRepository = ToolTypeRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(DeleteToolTypeCommand command)
        {
            var ToolType = ToolTypeRepository.GetById(command.ID);
            ToolTypeRepository.Delete(ToolType);
            unitOfWork.Commit();
            return new CommandResult(true);
        }


        public ICommandResult ExecuteRange(IEnumerable<DeleteToolTypeCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            ToolTypeRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<DeleteToolTypeCommand> commands)
        {
            throw new NotImplementedException();
        }
    }
}

